#include "stdafx.h"
#include "CppUnitTest.h"
#include "math.h"
#include <iostream>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;

namespace NativeTest_Question32
{
	class Mystack
	{
	public:
		Mystack(int size);
		~Mystack();
		void Push( int val);
		void Pop();
		int Top();
		bool Empty();

	private:
		int size;
		int* head;
		int index;
	};
	Mystack::Mystack(int size=100)
	{
		head = new int[size *3];
		this->size = size;
		index = -1;
	}
	Mystack::~Mystack()
	{
		//if(head != NULL)
		//	delete[] head;
	}
	void Mystack::Push(int val)
	{
		if(index == size - 1)
		{ // new array and copy
		}
		head[++index] = val;
		
	}
	void Mystack::Pop()
	{
		if(!Empty())
		{
			index--;
		}
	}
	int Mystack::Top()
	{
		if(!Empty())
			return head[index];
		else
			return INT_MIN;
	}
	bool Mystack::Empty()
	{
		return index== -1;
	}

	class StackCanMin
	{
	public:
		StackCanMin(int size);
		~StackCanMin();
		void Push(int val);
		int Top();
		void Pop();
		int Min();
	private:
		Mystack data;
		Mystack mindata;
	};

	StackCanMin::StackCanMin(int size)
	{
		data = Mystack(size);
		mindata = Mystack(size);
	}
	void StackCanMin::Push(int val)
	{
		data.Push(val);
		if(mindata.Empty())
		{
			mindata.Push(val);
		}
		else
		{
			mindata.Push(min(mindata.Top(), val));
		}
	}

	void StackCanMin::Pop()
	{
		if(!data.Empty())
		{
			data.Pop();
			mindata.Pop();
		}
	}

	int StackCanMin::Top()
	{
		if(!data.Empty())
			return data.Top();
	}

	int StackCanMin::Min()
	{
		return mindata.Top();
	}
	StackCanMin::~StackCanMin()
	{

	}

}
namespace NativeTest
{
	using namespace NativeTest_Question32;

	TEST_CLASS(Question32)
	{
	public:
		
		TEST_METHOD(Question32_TestMethod1)
		{
			StackCanMin s = StackCanMin(10);
			s.Push(1);
			s.Push(3);
			s.Push(5);
			s.Push(4);
			s.Push(2);
			s.Push(-1);
			
			Assert::AreEqual(-1, s.Min());
			s.Pop();
			Assert::AreEqual(1, s.Min());
		}
	};
}